package me.stone.stanimeclient.core.ui.binding.item

import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.launch

@Suppress("UNUSED")
abstract class ObserverFlowData(
    private val coroutineScope: CoroutineScope
) {

    class ObserverFlowField<T>(
        val mutableField: MutableStateFlow<T>
    ) : StateFlow<T> by mutableField

    protected fun <T> observerField(value: T) = ObserverFlowField(MutableStateFlow(value))

    fun <T> ObserverFlowField<T>.update(value: T) {
        if (this.value == value) return
        coroutineScope.launch { mutableField.emit(value) }
    }

}